# coding:utf-8
import logging
from logging.handlers import TimedRotatingFileHandler
# from logging.handlers import
import os

LOG_FORMAT = '%(asctime)s %(filename)s[line:%(lineno)d] %(name)s %(levelname)s %(message)s'
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
log_fmt = logging.Formatter(LOG_FORMAT,
                            DATETIME_FORMAT)
if os.environ.get('MIGONG') != 'PRODUCTION':
    streamHandler = logging.StreamHandler()
    streamHandler.setLevel(logging.DEBUG)
    handlers = [streamHandler]
else:
    handlers = []
if os.path.exists('/log') is True:
    log_path = os.path.join('/log', 'app.log')
else:
    log_path = 'app.log'

rotating = TimedRotatingFileHandler(log_path, when='D', interval=1, backupCount=30)
rotating.setFormatter(log_fmt)
rotating.setLevel(logging.INFO)
error = TimedRotatingFileHandler(log_path + '.error', when='D', interval=1, backupCount=30)
error.setFormatter(log_fmt)
error.setLevel(logging.ERROR)
handlers.append(rotating)
handlers.append(error)
logging.basicConfig(level=logging.DEBUG, handlers=handlers)


def get_logger(name=None):
    logger = logging.getLogger(name)
    return logger
